<template>
	<view class="abnormal-list-wrap">
		<!-- 头部 -->
		<view class="header">
			<uni-nav-bar :status-bar="true" fixed :title="title" leftIcon="back" @clickLeft="clickLeft"></uni-nav-bar>
		</view>
		<!-- 内容 -->
		<view class="content">
			<view class="view-content custom-view-content">
				<!--异常状态Tab-->
				<view class="exception-status-tab">
					<view class="status-type-wrap">
						<view class="status-list" ref="ULRef">
							<view class="status-item" :class="{'active': activeFlag === 'today'}" @click="switchStatus('today')">
								<view>今日异常</view>
								<view>({{todayNoHandle}})</view>
							</view>
							<view class="status-item" :class="{'active': activeFlag === 'history'}" @click="switchStatus('history')">
								<view>历史异常</view>
								<view>({{historyNoHandle}})</view>
							</view>
						</view>
					</view>
				</view>
				<view class="abnormal-content" ref="abnormalContent" v-if="abnormalList && abnormalList.length > 0">
					<view class="abnormal-item" v-for="(item, index) in abnormalList" :key="index">
						<view class="abnormal-details" @click="toCheckAbnormalDetails(item.exceptionId)">
							<view class="credentials-wrap" v-if="anomalousType === '01'">
								<view>{{expireTimeContent(item.content, 'content')}}</view>
								<view class="expired-text">{{expireTimeContent(item.content, 'expired')}}</view>
							</view>
							<view class="other-wrap" v-else>
								<view>{{item.content}}</view>
								<image v-if="item.viewroofFlag === '1'" src="../../static/img/common/has_img_icon.png" class="has-img-icon">
							</view>
							<view class="item-bottom">
								<view>{{item.departmentName}}</view>
								<view>{{item.occurrenceTime}}</view>
							</view>
						</view>
						<view class="dispose-status">
							<view class="dispose-name" @click="toCheckAbnormalDetails(item.exceptionId)">{{item.status | disposeStatus}}</view>
						</view>
					</view>
					<!-- 加载更多，没有数据，加载中 -->
					<uni-load-more :status="status" />
				</view>
				<!-- 没有异常 -->
				<view class="null-abnormal-wrap" v-else>
					<image class="icon_praise" src="../../static/img/common/icon_praise.jpg" mode=""></image>
					<view class="text-tips">当前无异常</view>
				</view>
			</view>
			<!-- 返回顶部 -->
			<view class="back-top-wrap" :style="{'display': showBackTop ? 'block' : 'none'}">
				<view  @click="backTop">
					<uni-icons type="arrowthinup" size="32" color="#fff"></uni-icons>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				departmentId: '',
				anomalousType: '',
				abnormalName: {
					'01': '证件异常',
					'02': '设备异常',
					'03': '人员异常',
					'04': '食品异常'
				},
				status: 'more',
				showBackTop: false,
				abnormalList: [],
				today: this.$utils.formatDate(new Date(), 'yyyy-MM-dd'),
				activeFlag: 'today',
				todayNoHandle: 0,
				historyNoHandle: 0,
				page: 1,
				pageSize: 15,
				totalPage: 0
			}
		},
		onLoad(option) {
			this.departmentId = option.departmentId
			this.anomalousType = option.anomalousType
			this.title = this.abnormalName[this.anomalousType] ? this.abnormalName[this.anomalousType] : '异常'
			this.ygstAppExceptionListGetFunc(this.departmentId, this.anomalousType, this.page, this.pageSize, this.today)
		},
		onPullDownRefresh() {
			uni.startPullDownRefresh()
			this.ygstAppExceptionListGetFunc(this.departmentId, this.anomalousType, this.page, this.pageSize, this.today)
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.page < this.totalPage) {
				this.status = 'more';
				this.ygstAppExceptionListGetFunc(this.departmentId, this.anomalousType, this.page++, this.pageSize, this.today)
			} else {
				this.status = 'noMore'
			}
		},
		onPageScroll(e) {
			e.scrollTop > 1000 ? this.showBackTop = true : this.showBackTop = false
		},
	    filters: {
		  disposeStatus(status) {
		    return status === '01' ? '处置' : '处置详情'
		  }
	    },
		computed: {
			expireTimeContent() {
			  return function (content, type) {
				let result = null
				let tmpContent = content
				let idx = tmpContent.indexOf('到期时间')
				if (type === 'content') {
				  idx > 0 ? result = tmpContent.substring(0, idx) : result = content
				} else {
				  idx > 0 ? result = tmpContent.substring(idx) : ''
				}
				return result
			  }
			}
		},
		methods: {
			clickLeft() {
				uni.navigateBack()
			},
			/**
			 * @description: 获取分类异常信息列表
			 * @author: LinW
			 * @createDate: 2019-08-13 11:15
			 * @param: mod
			 * @return:
			 */
			ygstAppExceptionListGetFunc(departmentId, anomalousType, page, pageSize, today) {
				let params = {
					departmentId,
					anomalousType,
					page,
					pageSize
				}
				// 今日异常传入当前时间，历史异常不必传入
				if (this.activeFlag === 'today') {
					params['today'] = today
				}
				this.status = 'loading';
				this.$request.ygstAppExceptionContractorListGet(params).then(res => {
					if (this.$utils.isEmptyObject(res)) {
						return
					}
					this.totalPage = res.totalPage
					this.todayNoHandle = res.todayNoHandle
					this.historyNoHandle = res.historyNoHandle
					this.abnormalList = this.abnormalList.concat(res.resultList)
				}).catch(e => {
					// 
				})
			},
			switchStatus(flag) {
				this.activeFlag = flag
				this.page = 1
				this.pageSize = 15
				this.totalPage = 0
				this.abnormalList.length = 0
				this.ygstAppExceptionListGetFunc(this.departmentId, this.anomalousType, this.page, this.pageSize, this.today)
			},
			backTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				})
				this.showBackTop = false
			},
			toCheckAbnormalDetails(exceptionId) {
				uni.navigateTo({
					url: `../abnormalDetails/abnormalDetails?anomalousType=${this.anomalousType}&exceptionId=${exceptionId}`
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.abnormal-list-wrap {
		.content {
			.custom-view-content {
				width: 100%;
				height: 100%;
				overflow-y: auto;
				background-color: #fff;
				display: flex;
				flex-direction: column;

				.exception-status-tab {
					padding: 20rpx 40rpx;

					/* 状态选择 */
					.status-type-wrap {
						width: 100%;
						box-sizing: border-box;
						display: flex;
						flex-direction: column;

						.status-list {
							box-sizing: border-box;
							display: flex;
							flex-flow: row nowrap;
							justify-content: center;
							align-items: center;

							.status-item {
								display: flex;
								justify-content: center;
								align-items: center;
								width: 100%;
								text-align: center;
								color: #44D696;
								font-size: 32rpx;
								border: 1px solid #44D696;
							}

							.status-item:first-child {
								border-top-left-radius: 30px;
								border-bottom-left-radius: 30px;
								border-right: none;
							}

							.status-item:last-child {
								border-top-right-radius: 30px;
								border-bottom-right-radius: 30px;
								border-left: none;
							}

							.active {
								color: #fff;
								background-color: #44D696;
							}
						}
					}
				}

				.null-abnormal-wrap {
					height: 300rpx;
					text-align: center;
					margin: 100rpx auto;
					position: relative;
					box-shadow: 0px 0px 0px 10rpx #ccc;
					border-radius: 20%;
					.icon_praise {
						width: 300rpx;
						height: 300rpx;
					}
					.text-tips {
						font-size: 30rpx;
						font-weight: bold;
						letter-spacing: 10rpx;
						color: green;
						transform: rotate(-40deg);
						position: absolute;
						bottom: 20%;
						right: 0%;
					}
				}

				.abnormal-content {
					background: #fff;
					flex: 1;
					height: 100%;
					overflow-y: auto;

					.abnormal-item {
						box-sizing: border-box;
						font-size: 32rpx;
						display: flex;
						justify-content: space-between;
						align-items: center;
						border-bottom: 1px solid #CCCCCC;
						.abnormal-details {
							padding: 10rpx 18px;
							flex: 1;

							.credentials-wrap {
								.expired-text {
									color: #ccc;
								}
							}

							.other-wrap {
								.has-img-icon {
									width: 28rpx;
									height: 24rpx;
								}
							}

							.item-bottom {
								margin-top: 24rpx;
								display: flex;
								justify-content: space-between;
								align-items: center;
								font-size: 30rpx;
								color: #ccc;
							}
						}

						.dispose-status {
							padding: 10rpx 18rpx;
							flex: 0 0 100rpx;
							border-left: 1px solid #ccc;

							.dispose-name {
								padding: 2rpx 4rpx;
								text-align: center;
								color: #43D995;
								border: 1px solid #43D995;
								border-radius: 10rpx;
							}
						}
					}
				}
			}
		}
		.back-top-wrap {
			display: none;
			width: 100rpx;
			height: 100rpx;
			text-align: center;
			background: rgba(0,0,0,.1);
			border-radius: 100%;
			position: fixed;
			right: 60rpx;
			top: 85%
		}
	}
</style>
